package com.qige.dome.homework;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author qige
 * @Date 2024-08-01 18:56
 * @Description
 */
public class homework1 {
    /*
    1.使用List和Map存放多个图书信息，遍历并输出。其中商品属性：编号，名称，单价，出版社；使用商品编号作为Map中的key。
     */
    public static void main(String[] args) {
        List<Book> books = new LinkedList<>();
        books.add(new Book("001", "Java", new BigDecimal(100), "XX出版社"));
        books.add(new Book("002", "C++", new BigDecimal(200), "YY出版社"));
        books.add(new Book("003", "Python", new BigDecimal(300), "ZZ出版社"));
        for (int i = 0; i < books.size(); i++) {
            System.out.println(books.get(i));
        }
        System.out.println("-------------------------------------");
        Map<String, Book> bookMap = new HashMap<>();
        bookMap.put("001", new Book("001", "Java", new BigDecimal(100), "XX出版社"));
        bookMap.put("002", new Book("002", "C++", new BigDecimal(200), "YY出版社"));
        bookMap.put("003", new Book("003", "Python", new BigDecimal(300), "ZZ出版社"));
        Set<String> keys = bookMap.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Book book = bookMap.get(key);
            System.out.println(key + "---" + book);
        }
    }
}
class Book {
    private String id;
    private String name;
    private BigDecimal price;
    private String publisher;

    public Book() {
    }

    public Book(String id, String name, BigDecimal price, String publisher) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.publisher = publisher;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", publisher='" + publisher + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;
        return Objects.equals(id, book.id) && Objects.equals(name, book.name) && Objects.equals(price, book.price) && Objects.equals(publisher, book.publisher);
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(id);
        result = 31 * result + Objects.hashCode(name);
        result = 31 * result + Objects.hashCode(price);
        result = 31 * result + Objects.hashCode(publisher);
        return result;
    }
}
